<?php
class Controller extends CController
{
    protected $_xsession;
    protected $_xcookies;
    /**
     * 初始化
     * @see CController::init()
     */
    public function init ()
    {
        $this->_xsession = new CHttpSession();
        $this->_xsession->open();
        $this->_xcookies = Yii::app()->request->getCookies();

    }
	public $layout='//layouts/column1';
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();

    /**
     * 2014 2 17
     */
    /**
     * 设置cookie
     */
    protected function _cookiesSet ($name = '', $value = '', $expire = 3600, $path = '', $domain = '', $secure = false)
    {
        $cookieSet = new CHttpCookie($name, $value);
        $expire && $cookieSet->expire = $expire;
        $path && $cookieSet->path = $path;
        $domain && $cookieSet->domain = $domain;
        $secure && $cookieSet->secure = $secure;
        Yii::app()->request->cookies[$name] = $cookieSet;
    }

    /**
     * 获取cookie
     */
    protected function _cookiesGet ($name, $once = false)
    {
        $cookie = Yii::app()->request->getCookies();
        if($cookie[$name]){
            $data = $cookie[$name]->value;
        }else{
            $data = '';
        }

        if ($once)
            unset($cookie[$name]);
        return $data;
    }

    /**
     * 清理cookie
     * @param  $name
     */
    protected function _cookiesRemove ($name)
    {
        $cookie = Yii::app()->request->getCookies();
        unset($cookie[$name]);
    }
    /**
     * 设置session
     *
     * @param data 数据,可以是数组
     */
    protected function _sessionSet ($name, $value = '', $expire = 0, $path = '')
    {
        $this->_xsession[$name] = $value;
    }

    /**
     * 获取session
     */
    protected function _sessionGet ($name, $once = false)
    {
        $data = $this->_xsession[$name];
        if ($once)
            $this->_xsession->remove($name);
        return $data;
    }

    /**
     * 清除session
     */
    protected function _sessionRemove ($name)
    {
        $this->_xsession->remove($name);
    }




    /**
     * 后台日志记录
     * @param  $intro
     */

    protected function _backendLogger (array $arr = array())
    {
        //查询日志是否记录
        if(Yii::app()->params['adminLog'] == 'open'){
            $model = new BackendLog();
            $model->attributes = $arr;
            !isset($arr['user_id']) && $model->user_id = intval(self::_sessionGet('_adminiUserId'));
            $model->url = Yii::app()->request->getRequestUri();
            $model->ip = Xtutuquan::getClientIP();
            $model->create_time = time();
            $model->save();
        }
    }

    /**
     * @author Asa_yang <88683231@qq.com>
     * @link http://www.hoohg.com/
     * @copyright Copyright &copy; 2011-2013 Hoohg Software LLC
     * @param string $length 随机hash的长度
     * @return hash 返回生成的哈希字符串
     *
     */
    //随机hash生成
    public function randStr($length=10){
        $hash='';
        $chars= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';   //@#!~?:-
        $max=strlen($chars)-1;
        mt_srand((double)microtime()*1000000);
        for($i=0;$i<$length;$i++){
            $hash.=$chars[mt_rand(0,$max)];
        }
        return $hash;
    }


    /**
     * 跳转页面 在基类中.
     * @author Asa_yang <88683231@qq.com>
     * @link http://www.hoohg.com/
     * @copyright Copyright &copy; 2011-2013 Hoohg Software LLC
     * @param string $message 弹窗提示文字 .
     * @param string $status 状态,为  success.
     * @param integer $time 跳转时间
     * @param string $url  跳转地址
     * @return html 返回弹窗界面
     */
    public function redirect_message($message='操作成功',$status='success',$time=3,$url=false){

        $back_color ='#ff0000';
        if($status =='success'){
            $back_color= 'blue';
        }elseif($status == 'error'){
            $back_color= 'red';
        }
        if(is_array($url)){
            $route=isset($url[0]) ? $url[0] : '';
            $url=$this->createUrl($route,array_splice($url,1));
        }
        if ($url)
        {
            $url = "window.location.href='{$url}'";
        }
        else
        {
            $url = "history.back();";
        }


        echo <<<HTML
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html>
			<head>
			<title>页面提示</title>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
			<style type="text/css" rel="stylesheet">
			body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, form, fieldset, textarea, p, blockquote, th, td, img, input, select {margin:0; padding:0;}
			dl, dt, dd, ul, ol, li{list-style:none; list-style-position:outside;}
			li, img {vertical-align:bottom;}
			.clear {font-size:0; line-height:0; height:0; clear:both; overflow:hidden;}
			.clearfix:after {clear: both;content: ".";display: block; height: 0;visibility: hidden;}
			* html .clearfix             { zoom: 1; } /* IE6 */
			*:first-child+html .clearfix { zoom: 1; } /* IE7 */
			/*全局*/
			body {background:#F4F4F4}
			.main {width:500px; position:absolute;left:50%; top:20%; margin:0 0 0 -250px; background:#FFFFFF; border:1px solid #DEDEDE; border-radius:5px; box-shadow:0 1px 3px #CCCCCC; padding:20px}
			h1 {font:normal 25px/50px "microsoft yahei"; color:#0085CC; margin:0 0 10px 0}
			#blue {color:#0085CC;}
			#red {color:-moz-activehyperlinktext};
			p {font:normal 14px/23px "microsoft yahei"; color:#797979}
			p a {margin:0 2px; color:#0055CC}
			</style>
			</head>
			<body >
				<div class="main">
				<h1 id="{$back_color}">{$message}</h1><br/><p>页面将在<span id="sec" style="color:{$back_color};">{$time}</span>秒内自动跳转，如果不想等待，请点击 <a href="javascript:history.back(-1);">这里</a>跳转。</p>
				<script type="text/javascript">
					var timer;
                    function run(){
                        var s = document.getElementById("sec");
                        if(s.innerHTML == 0){
                        	clearInterval(timer);
                        	{$url}
                            return false;
                        }
                        s.innerHTML = s.innerHTML * 1 - 1;

                    }
                    timer = window.setInterval("run();", 1000);
                </script>
			</div>
			</body>
			</html>

HTML;
        exit;

    }





}